package com.cuicuisha.gxyp.common.interceptor;

import com.alibaba.fastjson.JSON;
import com.cuicuisha.gxyp.model.entity.user.AuthContextUtil;
import com.cuicuisha.gxyp.model.entity.user.UserInfo;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

public class UserLoginAuthInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        // 如果token不为空，那么此时验证token的合法性
        String userJson = request.getHeader("userJson");
        if (StringUtils.hasText(userJson)) AuthContextUtil.setUser(JSON.parseObject(userJson , UserInfo.class));
        return true ;

    }

}